import { loginApi, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  // 初始化：从cookie中去读取token
  token: getToken()
}

const mutations = {
  // 设置token
  setToken(state,data) {
    state.token = data //存state
    setToken(data) // 存cookie
  },
  // 删除token
  removeToken(state) {
    state.token = null
    removeToken()
  }
}

const actions = {
 async loginAction(context,data) {
    let res = await loginApi(data)
    // console.log(res);
    context.commit('setToken', res)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

